package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.Attribute;
import com.cfets.annualAssessment.entity.Role;
import com.cfets.annualAssessment.mapper.RoleMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by liusonglin on 2016/11/25.
 */
@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    public List<Role> findRoleIdByUrl(String key) {
        return roleMapper.selectByUrl(key);
    }

    public List<Role> getRolePage(Role role) {
        Page<?> page = PageHelper.startPage(role.getPage(), role.getRows());
        List<Role> roles=null;
        roles = this.getRole(role);
        role.setTotalCount(page.getTotal());
        return roles;
    }

    public boolean newRole(Role role) {
        return roleMapper.insertSelective(role)>0;
    }

    public boolean updateRole(Role role) {
        return roleMapper.updateByPrimaryKey(role)>0;
    }

    public boolean deleteRoleById(int id) {
        return roleMapper.deleteByPrimaryKey(id)>0;
    }

    public List<Role> getRole(Role role){
        List<Role> roles=null;
        Example example=new Example(Role.class);
        example.setOrderByClause("post_time DESC");
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(role.getRoleName())){
            criteria.andLike("roleName","%"+role.getRoleName()+"%");
        }
        roles = roleMapper.selectByExample(example);
        return roles;
    }
}
